/*
 * mainwindow.cpp - main window of the application
 * Copyright (C) 2006-2007  Mithin Kumar L
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this application; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "../include/mainwindow.h"
#include "../include/titlebar.h"
#include "../include/tetrixboard.h"
#include "../include/scorewidget.h"

#include <QMouseEvent>
#include <QVBoxLayout>
#include <QIcon>

MainWindow::MainWindow(QWidget *parent) : QWidget(parent),buttonPressed(false)
{
	setFixedSize(280,540);
	setWindowFlags(Qt::FramelessWindowHint);
	setWindowOpacity(0.9);
	setAutoFillBackground(true);
	setWindowIcon(QIcon(":/images/cirris.png"));
	setWindowTitle(tr("Cirris"));

	QPalette pal;
	pal.setColor(QPalette::Window,QColor(10,10,10));
	setPalette(pal);

	//Lets make the widget curved
	QRegion maskRegion(10,10,260,520);
	maskRegion = maskRegion.unite(QRegion(0,10,10,520));
	maskRegion = maskRegion.unite(QRegion(270,10,10,520));
	maskRegion = maskRegion.unite(QRegion(10,0,260,10));
	maskRegion = maskRegion.unite(QRegion(10,530,260,10));
	maskRegion = maskRegion.unite(QRegion(0,0,20,20,QRegion::Ellipse));
	maskRegion = maskRegion.unite(QRegion(0,520,20,20,QRegion::Ellipse));
	maskRegion = maskRegion.unite(QRegion(260,0,20,20,QRegion::Ellipse));
	maskRegion = maskRegion.unite(QRegion(260,520,20,20,QRegion::Ellipse));

	setMask(maskRegion);

	titleBar = new TitleBar(this);
	connect(titleBar,SIGNAL(closePressed()),this,SLOT(close()));

	tetrixBoard = new TetrixBoard(this);
	tetrixBoard->installEventFilter(this);

	scoreWidget = new ScoreWidget(this);
	scoreWidget->installEventFilter(this);

	connect(tetrixBoard,SIGNAL(ringsRemoved(int)),scoreWidget,SLOT(ringsRemoved(int)));

	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	mainLayout->addWidget(titleBar);
	mainLayout->addWidget(tetrixBoard);
	mainLayout->addWidget(scoreWidget);

	setLayout(mainLayout);
}

void MainWindow::showEvent(QShowEvent *e)
{
	oPos = pos();
	QWidget::showEvent(e);
}

void MainWindow::hideEvent(QHideEvent *e)
{
	tetrixBoard->setGamePaused(true);
}

bool MainWindow::eventFilter(QObject *obj,QEvent *event)
{
	if(obj == tetrixBoard || obj == scoreWidget){
		if(event->type() == QEvent::MouseButtonPress){
			buttonPressed = true;
			QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
			oPos = mEvent->pos();
		}
		else if(event->type() == QEvent::MouseMove && buttonPressed){
			QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
			QPoint nPos = mEvent->pos();
			move(pos() - (oPos - nPos));
			oPos = nPos;
		}
		else if(event->type() == QEvent::MouseButtonRelease){
			buttonPressed = false;
		}
		return false;
	}
	else{
		return QWidget::eventFilter(obj,event);
	}
}